﻿using System;
using System.Collections.Generic;

namespace ListBook
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            var T = new Book("T", "天堂", 25, "张", "6000");
            var R = new Book("憨", "基地", 35, "张三", "8000");

            var books = new List<Book>(1) { T, R };

            books.Add(new Book("杠", "基地", 15, "三", "28000"));
            books.Add(new Book("狗1", "遗迹", 52, "无", "3000"));
            books.Add(new Book("狗2", "遗迹2", 5, "啊", "2525"));
            books.Add(new Book("狗3", "遗迹3", 10, "张三", "20"));
            books.Add(new Book("狗4", "遗迹4", 2, "无啊", "888"));
            books.Add(new Book("狗5", "遗迹5", 88, "啊无", "9999"));



            for (int i = 0; i < books.Count; i++)
            {
                Console.WriteLine(books[i]);
                Console.WriteLine();
            }

            int index1 = books.IndexOf(R);

            int index2 = books.FindIndex(r => r.Author == "张三");
            Console.WriteLine(index2);
            Console.WriteLine();

            Console.WriteLine("查询张三第一本书：");
            Book book = books.Find(r => r.Author == "张三");
            Console.WriteLine(book);
            Console.WriteLine();

            Console.WriteLine("查询张三所有书：");
            List<Book> beg = books.FindAll(r => r.Author == "张三");
            foreach (var item in beg)
            {
                Console.WriteLine($"{item:C}");
            }
            Console.WriteLine();

            Console.WriteLine("查看列表是否包含制定元素：");
            if (books.Exists(r => r.Author == "张三"))//List :确定系统是否。List<T>包含与指定谓词定义的条件相匹配的元素。
            {
                Console.WriteLine("成功");

            }
            else
            {
                Console.WriteLine("错误");
            }

            Console.WriteLine("- - - - - - - - - ");
            books.Sort(new Comparer(RComparer.Price));//对books的所有数据按照Price排序
            foreach (var i in books)
            {
                Console.WriteLine($"{i:A}");
            }

            Console.WriteLine("- - - - - - - - - ");

            int index5 = books.BinarySearch(0, 5, T, new Comparer(RComparer.Price));//BinarySearch:使用指定的比较器在整个已排序的 List<T> 中搜索元素，并返回该元素从零开始的索引。


        }
    }
}
